#include "threaddata.h"
#include "currentthread.h"
#include <sys/prctl.h>

ThreadData::ThreadData(const ThreadFunction &function, pid_t *processId, std::string &name, CountDownLatch *countDown):
    name(name),
    processId(processId),
    function(function),
    countDown(countDown)
{

}

void ThreadData::runInThread()
{
    // 获取进程编号
    *this->processId = CurrentThread::getThreadCacheId();

    this->processId = nullptr;

    this->countDown->countDown();

    this->countDown = nullptr;

    // 设置线程名称
    CurrentThread::threadName = name.empty() ? "Thread" : name.c_str();

    // 设置进程名字
    prctl(PR_SET_NAME, CurrentThread::threadName);

    // 线程回调函数
    this->function();

    CurrentThread::threadName = "finished";

}
